package com.logistics.controller.tenant;

import com.logistics.common.Result;
import com.logistics.entity.SysPermission;
import com.logistics.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 权限管理控制器
 */
@RestController
@RequestMapping("/tenant/permission")
public class PermissionController {
    
    @Autowired
    private PermissionService permissionService;
    
    /**
     * 获取权限树（用于角色权限分配）
     */
    @GetMapping("/tree")
    @PreAuthorize("hasAuthority('r3t8q5l9') or hasAnyRole('SUPER_ADMIN','SITE_ADMIN')")
    public Result<List<Map<String, Object>>> getPermissionTree() {
        List<SysPermission> permissions = permissionService.getPermissionTree();
        List<Map<String, Object>> tree = permissionService.buildPermissionTree(permissions);
        return Result.success(tree);
    }
    
    /**
     * 获取当前用户权限代码列表
     */
    @GetMapping("/user")
    public Result<List<String>> getUserPermissions(@RequestAttribute("userId") Long userId) {
        List<String> permissionCodes = permissionService.getUserPermissionCodes(userId);
        return Result.success(permissionCodes);
    }
    
    /**
     * 创建权限（预留）
     */
    @PostMapping
    @PreAuthorize("hasAnyRole('SUPER_ADMIN','SITE_ADMIN')")
    public Result<SysPermission> createPermission(@RequestBody SysPermission permission) {
        SysPermission created = permissionService.createPermission(permission);
        return Result.success(created);
    }
}

